package com.yyy.gui.listener;

import com.yyy.entity.Category;
import com.yyy.gui.pannel.CategoryPanel;
import com.yyy.gui.pannel.MainPanel;
import com.yyy.gui.pannel.RecordPannel;
import com.yyy.gui.pannel.SpendPanel;
import com.yyy.service.ReordService;
import com.yyy.util.GUIUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * @Author：yyy
 * @Package：com.yyy.gui.listener
 * @Project：HuTuBiill
 * @name：RecordListener
 * @Date：2024/11/12 21:32
 * @Filename：RecordListener
 * 记一笔按钮添加监听
 */
public class RecordListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        RecordPannel p=RecordPannel.instance;
        if(p.cbModel.cs.size()==0){
            JOptionPane.showMessageDialog(p,"暂无消费分类，无法添加，请先添加消费分类");
            MainPanel.instance.workingPanel.show(CategoryPanel.instance);
            return;
        }
        if(!GUIUtil.checkZero(p.tfSpend,"花费金额"))
            return;
        Category c=p.getSelectedCategory();
        int spend=Integer.parseInt(p.tfSpend.getText().trim());
        String comment=p.tfComment.getText().trim();
        Date d=p.datePicker.getDate();
        new ReordService().add(spend,c,comment,d);
        JOptionPane.showMessageDialog(p,"添加成功");
        MainPanel.instance.workingPanel.show(SpendPanel.instance);//切换到SpendPannel

    }
}
